﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM
{
    #region Using Statements

    using System.Collections.Generic;
    using System.Linq;
    
    #endregion

    /// <summary>
    /// The NameList interface provides the abstraction of an ordered 
    /// collection of parallel pairs of name and namespace values (which could
    /// be null values), without defining or constraining how this collection
    /// is implemented. The items in the NameList are accessible via an 
    /// integral index, starting from 0. 
    /// </summary>
    public class NameList
    {
        internal class NameListEntry
        {
            public string Name { get; set; }

            public string NamespaceURI { get; set; }
        }

        #region Properties

        internal List<NameListEntry> Entries { get; set; }

        #endregion

        #region Constructors

        public NameList()
        {
            Entries = new List<NameListEntry>();
        }

        #endregion

        #region DOM Level 3 Compliance

        /// <summary>
        /// Test if a name is part of this NameList. 
        /// </summary>
        /// <param name="str">The name to look for.</param>
        /// <returns>true if the name has been found, false otherwise.</returns>
        public bool contains(string str)
        {
            var test = Entries.Where(e => e.Name.Equals(str));

            return test.Count() > 0;
        }

        /// <summary>
        /// Test if the pair namespaceURI/name is part of this NameList.
        /// </summary>
        /// <param name="namespaceURI">The namespace URI to look for.</param>
        /// <param name="name">The name to look for. </param>
        /// <returns>true if the pair namespaceURI/name has been found, false otherwise.</returns>
        public bool containsNS(string namespaceURI, string name)
        {
            var test = Entries.Where(e => e.Name.Equals(name) && e.NamespaceURI.Equals(namespaceURI));

            return test.Count() > 0;
        }

        /// <summary>
        /// Returns the indexth name item in the collection.
        /// </summary>
        /// <param name="index">Index into the collection.</param>
        /// <returns>The name at the indexth position in the NameList, or null if there is no name for the specified index or if the index is out of range.</returns>
        public string getName(int index)
        {
            if (index > -1 && index < Entries.Count)
            {
                return Entries[index].Name;
            }
            return null;
        }

        /// <summary>
        /// Returns the indexth namespaceURI item in the collection.
        /// </summary>
        /// <param name="index">Index into the collection.</param>
        /// <returns>The namespace URI at the indexth position in the NameList, or null if there is no name for the specified index or if the index is out of range.</returns>
        public string getNamespaceURI(int index)
        {
            if (index > -1 && index < Entries.Count)
            {
                return Entries[index].Name;
            }
            return null;
        }

        #endregion
    }
}
